package com.ruoyi.controller.production;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.production.MesProductionReturn;
import com.ruoyi.service.production.IMesProductionReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生产退料单Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/production/return")
public class MesProductionReturnController extends BaseController {
    @Autowired
    private IMesProductionReturnService IMesProductionReturnService;

    /**
     * 查询生产退料单列表
     */
    @PreAuthorize("@ss.hasPermi('production:return:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesProductionReturn mesProductionReturn) {
        startPage();
        List<MesProductionReturn> list = IMesProductionReturnService.selectMesProductionReturnList(mesProductionReturn);
        return getDataTable(list);
    }

    /**
     * 导出生产退料单列表
     */
    @PreAuthorize("@ss.hasPermi('production:return:export')")
    @Log(title = "生产退料单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProductionReturn mesProductionReturn) {
        List<MesProductionReturn> list = IMesProductionReturnService.selectMesProductionReturnList(mesProductionReturn);
        ExcelUtil<MesProductionReturn> util = new ExcelUtil<MesProductionReturn>(MesProductionReturn.class);
        util.exportExcel(response, list, "生产退料单数据");
    }

    /**
     * 获取生产退料单详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:return:query')")
    @GetMapping(value = "/{returnId}")
    public AjaxResult getInfo(@PathVariable("returnId") Long returnId) {
        return success(IMesProductionReturnService.selectMesProductionReturnByReturnId(returnId));
    }

    /**
     * 新增生产退料单
     */
    @PreAuthorize("@ss.hasPermi('production:return:add')")
    @Log(title = "生产退料单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductionReturn mesProductionReturn) {
        // 校验退料单编号唯一性
        if (!IMesProductionReturnService.checkReturnCodeUnique(mesProductionReturn.getReturnCode(), mesProductionReturn.getReturnId())) {
            return error("新增生产退料单失败，退料单编号已存在");
        }
        return toAjax(IMesProductionReturnService.insertMesProductionReturn(mesProductionReturn));
    }

    /**
     * 修改生产退料单
     */
    @PreAuthorize("@ss.hasPermi('production:return:edit')")
    @Log(title = "生产退料单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductionReturn mesProductionReturn) {
        // 校验退料单编号唯一性（排除自身）
        MesProductionReturn existReturn = IMesProductionReturnService.selectMesProductionReturnByReturnId(mesProductionReturn.getReturnId());
        if (existReturn != null && !existReturn.getReturnCode().equals(mesProductionReturn.getReturnCode())) {
            if (!IMesProductionReturnService.checkReturnCodeUnique(mesProductionReturn.getReturnCode(), mesProductionReturn.getReturnId())) {
                return error("修改生产退料单失败，退料单编号已存在");
            }
        }
        return toAjax(IMesProductionReturnService.updateMesProductionReturn(mesProductionReturn));
    }

    /**
     * 删除生产退料单
     */
    @PreAuthorize("@ss.hasPermi('production:return:remove')")
    @Log(title = "生产退料单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{returnIds}")
    public AjaxResult remove(@PathVariable Long[] returnIds) {
        return toAjax(IMesProductionReturnService.deleteMesProductionReturnByReturnIds(returnIds));
    }

    /**
     * 校验退料单编号是否唯一
     */
    @GetMapping("/checkReturnCodeUnique")
    public AjaxResult checkReturnCodeUnique(String returnCode, Long returnId) {
        boolean isUnique = IMesProductionReturnService.checkReturnCodeUnique(returnCode, returnId);
        return AjaxResult.success(isUnique);
    }

    /**
     * 根据工单ID查询退料单列表
     */
    @PreAuthorize("@ss.hasPermi('production:return:list')")
    @GetMapping("/listByWorkOrderId/{workOrderId}")
    public AjaxResult listByWorkOrderId(@PathVariable Long workOrderId) {
        List<MesProductionReturn> list = IMesProductionReturnService.selectReturnListByWorkOrderId(workOrderId);
        return success(list);
    }

    /**
     * 根据领料单ID查询退料单列表
     */
    @PreAuthorize("@ss.hasPermi('production:return:list')")
    @GetMapping("/listByPickingId/{pickingId}")
    public AjaxResult listByPickingId(@PathVariable Long pickingId) {
        List<MesProductionReturn> list = IMesProductionReturnService.selectReturnListByPickingId(pickingId);
        return success(list);
    }

    /**
     * 审核生产退料单
     */
    @PreAuthorize("@ss.hasPermi('production:return:audit')")
    @Log(title = "生产退料单审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{returnId}/{status}")
    public AjaxResult audit(@PathVariable Long returnId, @PathVariable String status) {
        return toAjax(IMesProductionReturnService.auditMesProductionReturn(returnId, status));
    }

    /**
     * 执行生产退料单
     */
    @PreAuthorize("@ss.hasPermi('production:return:execute')")
    @Log(title = "生产退料单执行", businessType = BusinessType.UPDATE)
    @PutMapping("/execute/{returnId}")
    public AjaxResult execute(@PathVariable Long returnId) {
        return toAjax(IMesProductionReturnService.executeMesProductionReturn(returnId));
    }
}